package com.liujie.app.service;

import com.liujie.app.dao.BackendUserDao;
import com.liujie.app.dto.LoginDto;
import com.liujie.app.entity.BackendUser;
import com.liujie.app.utils.SessionUtils;
import com.liujie.app.vo.Result;
import com.liujie.app.vo.TokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class BackenduserService {
    @Autowired
    BackendUserDao backendUserDao;

    public Result getLogin(LoginDto loginDto) {
        BackendUser backendUser = new BackendUser();
        backendUser.setUsercode(loginDto.getUsername());
        backendUser.setUserpassword(loginDto.getPassword());
        backendUser.setId(loginDto.getId());

        List<BackendUser> list = backendUserDao.queryAllByLimit(backendUser);
        if (list.size() == 0) {
            return Result.error("账号密码错误！");
        }

        String token = UUID.randomUUID().toString().replace("-","");
        token+="-"+list.get(0).getUsercode();
        token+="-admin";
        token+="-"+list.get(0).getId();
        token+="-"+System.currentTimeMillis();

        TokenVo tokenVo=new TokenVo(list.get(0).getId(),token,list.get(0).getUsercode(),"admin");
        SessionUtils.put(token,tokenVo);
        return Result.success(tokenVo);
    }

}
